package com.lngsyz.eseal.base.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Base64;
import java.util.Random;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

import com.lngsyz.eseal.base.entity.util.CaptchaEntity;


public class CaptchaGenerator {
	
	private static BufferedImage drawImage(String randomStr) {
		// 1.画什么
		// 随机数生成
		// 2.确定颜色
		Random random = new Random();
		// 背景色：底色（0~255）
		int red = random.nextInt(50);
		int green = random.nextInt(190);
		int blue = random.nextInt(200);
		Color bgColor = new Color(red, green, blue);
//		        System.out.println(red + " " + green + " " + blue);
		// 前景色：人物，文字）
		int red2 = 255 - bgColor.getRed();
		int green2 = 255 - bgColor.getGreen();
		int blue2 = 255 - bgColor.getBlue();
		Color foreColor = new Color(red2, green2, blue2);
		// 3.开始画画（二维码）
		// 3.1准备画纸（大小）
		BufferedImage bufferedImage = new BufferedImage(80, 30, BufferedImage.TYPE_INT_RGB);
		// 3.2 准备画笔
		Graphics graphics = bufferedImage.getGraphics();
		// 3.3 画纸涂满底色
		graphics.setColor(bgColor);
		graphics.fillRect(0, 0, 80, 30);
		// 3.4 画笔换成前景色
		graphics.setColor(foreColor);
		graphics.setFont(new Font("黑体", Font.BOLD, 26));
		graphics.drawString(randomStr, 10, 28);
		// 画50个噪点
		for (int i = 0; i < 50; ++i) {
			graphics.setColor(Color.white);
			int x = random.nextInt(80);
			int y = random.nextInt(30);
			graphics.fillRect(x, y, 1, 1);
		}
		return bufferedImage;
	}
	
	private static String randomCode() {
		Random random = new Random();
		String str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
		// 存生成的字母
		String randomStr = "";
		for (int i = 0; i < 4; ++i) {
			// 拿一个数据的位置
			int index = random.nextInt(str.length());
			char letter = str.charAt(index);
			randomStr += letter;
		}
		return randomStr;
	}
	
	private static String img2Base64(BufferedImage bufferedImage) throws Exception {
		// 将 BufferedImage 对象转换为 byte 数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", baos);
        byte[] bytes = baos.toByteArray();
        
        // 将 byte 数组编码为 base64 字符串
        String base64Str = Base64.getEncoder().encodeToString(bytes);
        return base64Str;
	}
	
	private static void img2Local(BufferedImage bufferedImage, String path) throws Exception {
		if(StringUtils.isBlank(path)) {
			path = "D:/code.jpg";
		}
		// 3.5 展示观众
		FileOutputStream fileOutputStream = new FileOutputStream(path);
		// 变成文件
		ImageIO.write(bufferedImage, "jpeg", fileOutputStream);
		System.out.println("二维码生成成功");
	}
	
	public static CaptchaEntity drawBase64() throws Exception {
		try {
			CaptchaEntity rtn = new CaptchaEntity();
			String randomStr = randomCode();
			BufferedImage bufferedImage = drawImage(randomStr);
			String base64Str = img2Base64(bufferedImage);
			rtn.setCode(randomStr);
			rtn.setBase64Str(base64Str);
			return rtn;
		} catch(Exception e) {
			return null;
		}
	}
	
	public static void drawLocal(String path) throws Exception {
		try {
			String randomStr = randomCode();
			BufferedImage bufferedImage = drawImage(randomStr);
			img2Local(bufferedImage, path);
		} catch(Exception e) {
		}
	}
}